//
//  TaskProcessViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/9.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  任务流程主界面

#import "TaskProcessViewController.h"
#import "TaskProcessTableViewCell.h"
#import "TaskProgressModel.h"
#import "AddTaskProcessViewController.h"
#import "EditTaskProessViewController.h"
#import "EditTaskViewController.h"

static NSString *TaskProcessTableViewCellIdentifier=@"TaskProcessTableViewCellIdentifier";
@interface TaskProcessViewController ()<UITableViewDataSource,UITableViewDelegate,TaskProcessTableViewCellDelegate>

@property(nonatomic,strong)UITableView *tableView;

//@property(nonatomic,strong)NSMutableArray *taskProgressArr;

@property(nonatomic,assign)BOOL isAdd;

@property(nonatomic,strong)UIButton *addBtn;

@end

@implementation TaskProcessViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self GetData];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.delegate && [self.delegate respondsToSelector:@selector(currentTaskProgressArr:)])
    {
        [self.delegate currentTaskProgressArr:self.taskProgressArr];
    }
}

// 编辑流程
-(void)editTaskProcessWithTaskProgressModel:(TaskProgressModel *)model cell:(TaskProcessTableViewCell *)cell
{
    __block BOOL isEdit=NO;
    [model.opts enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         if ([obj isEqualToString:@"edit"])
         {
             isEdit=YES;
             *stop=YES;
         }
     }];

    if (isEdit)
    {
        // 编辑任务流程界面
        EditTaskProessViewController *vc=[EditTaskProessViewController new];
        vc.model=model;
        [self.navigationController pushViewController:vc animated:NO];
    }
    else
    {
        [self.view makeToast:@"没权限操作" duration:.5 position:CSToastPositionCenter];
    }
}

// 删除流程
-(void)deleteTaskProcessWithTaskProgressModel:(TaskProgressModel *)model cell:(TaskProcessTableViewCell *)cell
{
    __block BOOL isDelete=NO;
    [model.opts enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isEqualToString:@"delete"])
        {
            isDelete=YES;
            *stop=YES;
         }
    }];

    if (isDelete)
    {
        UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要删除它吗？"  cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex)
        {
            if (buttonIndex==0)
            {
                [API DeleteTaskProgressWithStepID:model.step_id success:^(id res) {

                    if ([res[@"rt"] isEqual:@(1)])
                    {
                        [self.view makeToast:@"流程删除成功" duration:1 position:CSToastPositionCenter];
                    }
                    else
                    {
                        [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                    }

                    [self GetData];
                    
                } failure:^(NSInteger stateCode) {
                    
                }];
            }
        } otherButtonTitles:@"确定",nil];
        [alert show];
    }
    else
    {
       [self.view makeToast:@"没权限操作" duration:.5 position:CSToastPositionCenter];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"流程";

    _taskProgressArr=[NSMutableArray array];

     [self.view addSubview:self.tableView];

//    [self addProjectBtn];

    [self isAddTaskProgress];

}

-(void)GetData
{
    self.tableView.mj_header=[MJRefreshNormalHeader
                              headerWithRefreshingBlock:^{
                                  [self GetTaskProgressListData];
                              }];
    [self.tableView.mj_header beginRefreshing];

}

// 是否显示添加流程按钮
-(void)isAddTaskProgress
{
    if (([self.taskStatus integerValue]==6)||([self.taskStatus integerValue]==7)||([self.taskStatus integerValue]==8)||([self.taskStatus integerValue]==9)||([self.taskStatus integerValue]==10))
    {
        // 添加流程按钮不显示
    }
    else
    {
        [self addProjectBtn];
    }

#warning 处理
////    if (_isAdd)
//    {
//        [self addProjectBtn];
//    }

}


// 添加
-(void)addProjectBtn
{
    _addBtn=[AppUI createButtonWithSupeView:self.view andWithNormalImage:[UIImage imageNamed:@"add"] andWithSelectImage:[UIImage imageNamed:@"add"] andWithBtnAction:^{

        // 跳转到 任务流程添加 界面
        AddTaskProcessViewController *vc=[AddTaskProcessViewController new];
        vc.isFirst=(self.taskProgressArr.count>0)?NO:YES;
        vc.startTimeStr=self.startTimeStr;
        TaskProgressModel *model=[self.taskProgressArr lastObject];
        vc.model=model;
        vc.taskID=self.taskID;

        [self.navigationController pushViewController:vc animated:NO];

    }];
    [self.view addSubview:_addBtn];
    _addBtn.frame=CGRectMake(APPW-kmargin*8,APPH-kmargin*10,kmargin*6,kmargin*6);
}

-(void)GetTaskProgressListData
{
    [self.taskProgressArr removeAllObjects];

    // 获取流程列表
    [API GetTaskProgressListWithTaskID:self.taskID success:^(id res) {

        NSLog(@"sjsjdjfdjfdj---%@",res);
        
      if ([res[@"rt"] isEqual:@(1)])
      {
          if ([res[@"rows"] count]>0)
          {
              NSMutableArray *arr=[NSMutableArray array];
              for (NSDictionary *dic in res[@"rows"])
              {
                  TaskProgressModel *model=[TaskProgressModel yy_modelWithDictionary:dic];
                  [arr addObject:model];
                  if ([model.user_id isEqualToString:@"0"])
                  {
                      self.addBtn.hidden=YES;
                  }
                  else
                  {
                      self.addBtn.hidden=NO;
                  }
              }
              self.taskProgressArr=arr;
          }
          else
          {
              [self.view makeToast:@"暂无数据" duration:1 position:CSToastPositionCenter];
          }
          // 是否显示添加流程按钮
//          [self isAddTaskProgress];
      }
      else
      {
          [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
      }

        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];

    } failure:^(NSInteger stateCode) {

          [self.tableView.mj_header endRefreshing];
        
    }];
}

// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=MAIN_BACK_COLOR;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
     [self.tableView registerClass:[TaskProcessTableViewCell class] forCellReuseIdentifier:TaskProcessTableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.taskProgressArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TaskProcessTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TaskProcessTableViewCellIdentifier];
    cell.delegate=self;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.row==0)
    {
        cell.topLine.hidden=YES;
        if (self.taskProgressArr.count==1)
        {
          cell.bottomLine.hidden=YES;
        }
        else
        {
            cell.bottomLine.hidden=NO;
        }
    }
    else if (indexPath.row==[self.taskProgressArr count]-1)
    {
        cell.bottomLine.hidden=YES;
    }
    cell.model=self.taskProgressArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*18;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin*4;
}

@end
